package tk.mynoteapp.dev.demo04.genericobjectpool;

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import redis.clients.jedis.Jedis;

public class MyJedisFactory extends BasePooledObjectFactory<Jedis> {
    private String host;
    private int port;

    public MyJedisFactory(String host, int port) {
        this.host = host;
        this.port = port;
    }

    @Override
    public PooledObject<Jedis> makeObject() throws Exception {
        Jedis jedis = new Jedis(host, port);
        jedis.connect();
        System.out.println(jedis.isConnected());
        return new DefaultPooledObject<>(jedis);
    }

    @Override
    public void destroyObject(PooledObject<Jedis> p) throws Exception {
        p.getObject().close();
    }

    @Override
    public boolean validateObject(PooledObject<Jedis> p) {
        if (p.getObject().isConnected()) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Jedis create() throws Exception {
        return null;
    }

    @Override
    public PooledObject<Jedis> wrap(Jedis jedis) {
        return null;
    }
}
